package com.example.openglesex;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

	public static final float FULL_X_ANGLE = 360;
	public static final float FULL_Y_ANGLE = 720;
	
	private float mX;
	private float mY;
	private GLSurfaceView mSurface;
	private GlRenderer mRenderer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSurface = new GLSurfaceView(this);
       // mSurface.setEGLContextClientVersion(2);
        mRenderer = new GlRenderer(this);
        mSurface.setRenderer(mRenderer);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(mSurface);
    }

    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	int action = event.getAction();
    	float x = event.getX();
    	float y = event.getY();
    	switch (action) {
		case MotionEvent.ACTION_DOWN:
			mX = x;
			mY = y;
			break;
		case MotionEvent.ACTION_MOVE:
			float dx = x - mX;
			float dy = y - mY;  //openGL的Y轴与activity的相反
			float rateX = dx/mRenderer.mWidth;
			float rateY = dy/mRenderer.mHeight;
			
			//上下滑动是绕X轴转，左右滑动是绕Y轴转
			mRenderer.mAngleY = FULL_X_ANGLE * rateX;
			mRenderer.mAngleX = FULL_Y_ANGLE * rateY;
			mX = x;
			mY = y;
			break;
		default:
		case MotionEvent.ACTION_UP:
			mRenderer.computRay(x,y);
			break;
		}
    	
    	
    	return super.onTouchEvent(event);
    }
}
